﻿using System;
using System.Data;
using THANHTUNGO.PORTAL.Components.Configuration;
using THANHTUNGO.PORTAL.Components.UI;

namespace THANHTUNGO.PORTAL.Modules.MODULE.Controls
{
    public partial class Installer : AddEditItemControl
    {
        private int defId = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Calculate security defId
            if (Request.Params["defid"] != null)
            {
                defId = Int32.Parse(Request.Params["defid"]);
            }

            // If this is the first visit to the page, bind the definition data 
            if (Page.IsPostBack == false)
            {
                if (defId == -1)
                {
                    // new module definition
                    txtModuleName.Text = "[Tên module]";
                    txtSourceFile.Text = "~/Modules/[Module Folder]/Main.ascx";
                }
                else
                {
                    // Obtain the module definition to edit from the database
                    PortalConfiguration config = new PortalConfiguration();
                    PortalDataSet.ModuleDefinitionRow modDefRow = config.GetSingleModuleDefinition(defId);

                    // Read in information
                    txtModuleName.Text = modDefRow.Name;
                    txtSourceFile.Text = modDefRow.SourceFile;
                    txtGUID.Text = modDefRow.GeneralDefinitionID.ToString();
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                PortalConfiguration config = new PortalConfiguration();

                if (defId == -1)
                {
                    // Obtain Portal from Current Context
                    Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)Context.Items["Portal"];

                    // Add a new module definition to the database
                    config.AddModuleDefinition(portal.ID, txtModuleName.Text, txtSourceFile.Text, new Guid(txtGUID.Text));
                }
                else
                {
                    // update the module definition
                    config.UpdateModuleDefinition(defId, txtModuleName.Text, txtSourceFile.Text, new Guid(txtGUID.Text));
                }

                // Redirect back to the portal admin page
                Response.Redirect("~/Default.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            // delete definition
            PortalConfiguration config = new PortalConfiguration();
            config.DeleteModuleDefinition(defId);

            // Redirect back to the portal admin page
            Response.Redirect("~/Default.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID);
        }

        protected void btnReadXml_Click(object sender, EventArgs e)
        {
            string xmlPath = Server.MapPath(txtXmlPath.Text);
            DataSet ds = new DataSet();
            ds.ReadXml(xmlPath);
            DataRow row = ds.Tables[0].Rows[0];
            txtModuleName.Text = row["Name"].ToString();
            txtSourceFile.Text = row["SourceFile"].ToString();
            txtGUID.Text = row["GeneralDefinitionID"].ToString();
        }
    }
}